home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DClap / dgg.c < prev    next >
Text File  |  1996-07-05  |  8KB  |  344 lines

  1. /* dgg.c 
  2.    additions to ncbi tool library
  3.    d.g.gilbert
  4. */
  5.  
  6. #include <dgg.h>
  7. #include <ncbi.h>
  8.  
  9.  
  10.  
  11. #ifdef OS_MAC
  12. char* LineEnd = "\015";
  13. short LineEndSize = 1;
  14. #endif
  15.  
  16. #ifdef OS_UNIX
  17. char* LineEnd = "\012";
  18. short LineEndSize = 1;
  19. #endif
  20.  
  21. #ifdef OS_DOS
  22. /* do msdos apps now deal w/ lf only as lineend ??  */
  23. char* LineEnd = "\012";
  24. short LineEndSize = 1;
  25. /*char* LineEnd = "\015\012";  */
  26. /*short LineEndSize = 2; */
  27. #endif
  28.  
  29. /* I don't know what VMS/ohters use -- guess at LF */
  30.  
  31. #if !defined(OS_MAC) && !defined(OS_UNIX) && !defined(OS_DOS)
  32. char* LineEnd = "\012";
  33. short LineEndSize = 1;
  34. #endif
  35.  
  36.  
  37. /* damn stupid sun/gcc linker can't find these two in libvibrant.a... */
  38. void Dgg_IntToStr (Nlm_Int2 intval, Nlm_CharPtr str,
  39.                           Nlm_Int2 length, ulong maxsize)
  40. {
  41.   Nlm_Char  temp [80];
  42.  
  43.   sprintf (temp, "%*d", length, intval);
  44.   Nlm_StringNCpy (str, temp, maxsize);
  45. }
  46.  
  47. void Dgg_LongToStr (Nlm_Int4 longval, Nlm_CharPtr str,
  48.                            Nlm_Int2 length, ulong maxsize)
  49. {
  50.   Nlm_Char  temp [80];
  51.  
  52.   sprintf (temp, "%*ld", length, longval);
  53.   Nlm_StringNCpy (str, temp, maxsize);
  54. }
  55.  
  56.  
  57.  
  58. void Dgg_StrUpcase( char FAR *s)
  59. {
  60.     if (s) for ( ; *s; s++) *s= to_upper(*s);
  61. }
  62.  
  63. void Dgg_StrLocase( char FAR *s)
  64. {
  65.     if (s) for ( ; *s; s++) *s= to_lower(*s);
  66. }
  67.             
  68.  
  69. long LIBCALL Dgg_Str2Idtype( char FAR *s)
  70. {
  71.     /* arrrggghhhh -- 16bit os needs typecast to long */
  72.     if (s) {
  73.         long id;
  74.         id= ((((long)s[0])<<24) | (((long)s[1])<<16) | (((long)s[2])<<8) | s[3]);
  75.         return id;
  76.         }
  77.     else return 0;
  78. }
  79.  
  80.         
  81. const Nlm_CharPtr LIBCALL Dgg_Idtype2Str( long id)
  82. {
  83.     static char buf[5];
  84.     buf[0] = (id>>24) & 0xff;
  85.     buf[1] = (id>>16) & 0xff;
  86.     buf[2] = (id>> 8) & 0xff;
  87.     buf[3] = (id    ) & 0xff;
  88.     buf[4]= '\0';
  89.     return buf;
  90. }
  91.  
  92. Nlm_CharPtr LIBCALL Dgg_StrPrependCat( Nlm_CharPtr *dest, char FAR *src)
  93. {
  94.     long newlen, oldlen;
  95.     if (dest == NULL) return NULL;
  96.     newlen= Nlm_StrLen( src);
  97.     if (*dest) {
  98.         oldlen= Nlm_StrLen( *dest);
  99.         *dest= (Nlm_CharPtr) Nlm_MemMore( *dest, oldlen+newlen+1);
  100.         if (*dest) {
  101.             Nlm_MemMove( *dest + newlen, *dest, oldlen+1);
  102.             Nlm_MemCopy( *dest, src, newlen);
  103.             }
  104.         }
  105.     else {
  106.         *dest= (Nlm_CharPtr) Nlm_MemDup( src, newlen);
  107.         }
  108.     return *dest;        
  109. }
  110.  
  111. Nlm_CharPtr LIBCALL Dgg_StrExtendCat( Nlm_CharPtr *dest, char FAR *src)
  112. {
  113.     long newlen, oldlen;
  114.     if (dest == NULL) return NULL;
  115.     newlen= 1 + Nlm_StrLen( src);
  116.     if (*dest) {
  117.         oldlen= Nlm_StrLen( *dest);
  118.         *dest= (Nlm_CharPtr) Nlm_MemMore( *dest, oldlen+newlen);
  119.         if (*dest) Nlm_MemCopy( *dest+oldlen, src, newlen);
  120.         }
  121.     else {
  122.         *dest= (Nlm_CharPtr) Nlm_MemDup( src, newlen);
  123.         }
  124.     return *dest;        
  125. }
  126.  
  127.  
  128. Nlm_CharPtr LIBCALL Dgg_MemExtendCat( Nlm_CharPtr *dest, long dlen, char FAR *src, long slen)
  129. {
  130.     if (dest == NULL) return NULL;
  131.     if (*dest) {
  132.         *dest= (Nlm_CharPtr) Nlm_MemMore( *dest, dlen+slen);
  133.         if (*dest) Nlm_MemCopy( *dest+dlen, src, slen);
  134.         }
  135.     else {
  136.         *dest= (Nlm_CharPtr) Nlm_MemDup( src, slen);
  137.         }
  138.     return *dest;        
  139. }
  140.  
  141.  
  142. Nlm_CharPtr LIBCALL  Dgg_StrAppend (char FAR *to,  char FAR *from)
  143. {
  144.     if (to) {
  145.      long fromlen, tolen;
  146.      if (!from) return to;
  147.      fromlen= 1+StrLen(from);
  148.      tolen= StrLen(to);
  149.      to= (Nlm_CharPtr) MemMore( to, tolen+fromlen);
  150.      MemCopy( to+tolen, from, fromlen);
  151.      return to;
  152.      }
  153.     else
  154.      return NULL;
  155. }
  156.  
  157.  
  158.  
  159. /*****************************************************************************
  160. *
  161. *   DCLAP needs to be able to move prefs data from distribution file to
  162. *   user prefs file.
  163. *
  164. *    Result is list of all valid prefs lines from file.
  165. *   Section name lines start w/ '[', name == line+1
  166. *   Variable names include a '=' between name & paramaters
  167. *   Comment lines are ignored
  168. *   Recommend using [version=123] to signify new versions of prefs
  169. *   data in same file.
  170. *
  171. *   This is from NCBI's coretools/ncbienv.c: Nlm_ReadConfigFile (fp)
  172. *
  173. *****************************************************************************/
  174.  
  175. #ifdef TESTING__MWERKS__
  176. /* testing bug in fgets -- trashed data after a while... */
  177. char* dgFgets( char* str, ulong strsize, FILE* fp)
  178. {
  179. #ifdef __MPW__
  180. #define kNewline '\r'
  181. #else
  182. #define kNewline '\n'
  183. #endif
  184.  
  185.     register int c = 0;
  186.     register char* cp = str;
  187.     
  188.     if (!strsize) return NULL;
  189.     c= getc( fp);
  190.     strsize--;
  191.     if (c == EOF) return NULL;
  192.           
  193.     while (c != EOF && strsize) {
  194.         *cp++= c;
  195.         strsize--;
  196.         if (c == kNewline) strsize= 0;
  197.         if (strsize) c= getc( fp);
  198.         }
  199.     *cp= 0;
  200.     return str;
  201. }
  202.  
  203. #else
  204. #define dgFgets(a,b,c) fgets(a,b,c)
  205. #endif
  206.  
  207.  
  208. extern Nlm_CharPtr* Dgg_ReadDefaultPrefs(FILE *fp, Nlm_Int2* nlines)
  209. {
  210.   Nlm_Char           ch;
  211.   Nlm_Char          str [256];
  212.   Nlm_CharPtr          tmp, mid;
  213.   Nlm_CharPtr    *      linelist;
  214.   Nlm_Int2               nlin, maxl;
  215.   
  216.   nlin = 0;
  217.   linelist= NULL;
  218.   if (fp) {
  219.     maxl= 50;
  220.       linelist= (Nlm_CharPtr*) Nlm_MemNew(maxl * sizeof(Nlm_CharPtr));
  221.     while (linelist && dgFgets (str, sizeof(str), fp)) {
  222.       ch = *str;
  223.       
  224.          if (ch < ' ' || ch == ';') {  /* ch == '\n' || ch == '\r' */ 
  225.              /* empty lines & comments -- skip */
  226.              }
  227.              
  228.         else if (ch == '[') {
  229.           /* this is all we want -- dgg */
  230.              tmp = str;
  231.              while (ch != '\0' && ch != ']') {
  232.                  tmp++;
  233.                  ch = *tmp;
  234.                }
  235.              *tmp = '\0';
  236.           nlin++;
  237.           if (nlin>=maxl) {
  238.               maxl= nlin+50;
  239.                   linelist= (Nlm_CharPtr*) Nlm_MemMore( linelist, maxl*sizeof(Nlm_CharPtr));
  240.                   }
  241.           if (linelist) linelist[ nlin-1 ]= Nlm_StringSave( str ); /* + 1);  -- ! leave leading '[' as key to sect */
  242.         }
  243.         
  244.              else {
  245.             tmp = str;
  246.           mid = NULL;
  247.           while (ch != '\0' && ch != '\n' && ch != '\r') {
  248.               if (ch == '=') {
  249.                    mid = tmp;
  250.                    /* *mid = '\0'; */
  251.                  mid++;
  252.                    }
  253.               tmp++;
  254.                ch = *tmp;
  255.                  }
  256.             *tmp = '\0';
  257.                  if (tmp>str) { /* was: if (mid) */
  258.                      /* ?? save only variable=parameters lines - !!NO need 'var' alone to remove old vars*/
  259.                nlin++;
  260.               if (nlin>=maxl) {
  261.                   maxl= nlin+50;
  262.                       linelist= (Nlm_CharPtr*) Nlm_MemMore( linelist, maxl*sizeof(Nlm_CharPtr));
  263.                       }
  264.               if (linelist) linelist[ nlin-1 ]= Nlm_StringSave( str);        
  265.                     }
  266.                  
  267.                  }
  268.         }
  269.       }
  270.   *nlines= nlin;
  271.   return linelist;
  272. }
  273.  
  274.  
  275.  
  276.  
  277.  /* damn dummy funcs for damn libXm which can't find damn /usr/ccs/lib/libgen funcs */
  278.  
  279.  
  280. char * regcmp(const char *a, ...)
  281.  {
  282.    return NULL;
  283.  }
  284.  
  285. char * regex(const char *a, const char *b, ...)
  286.  {
  287.      return NULL;
  288.  }
  289.  
  290.  
  291.  
  292. #ifdef OS_MAC
  293.  
  294.     // undef some conflicts b/n Types.h && NlmTypes
  295. #undef Handle
  296. //#undef true
  297. //#undef false
  298. //#undef Boolean
  299. #include <Types.h>
  300. #include <Memory.h>
  301. #include <OSUtils.h>
  302. #include <Files.h>
  303. #include <Folders.h>
  304.  
  305. pascal    short    DetermineVRefNum( char* pathname, short vRefNum, short *realVRefNum)
  306. {
  307.     HParamBlockRec pb;
  308.     Str255 tempPathname;
  309.     short error;
  310.  
  311.     pb.volumeParam.ioVRefNum = vRefNum;
  312.     if ( pathname == NULL ) {
  313.         pb.volumeParam.ioNamePtr = NULL;
  314.         pb.volumeParam.ioVolIndex = 0;        /* use ioVRefNum only */
  315.         }
  316.     else {
  317.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  318.         pb.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  319.         pb.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  320.         }
  321.     error = PBHGetVInfoSync(&pb);
  322.     *realVRefNum = pb.volumeParam.ioVRefNum;
  323.     return ( error );
  324. }
  325.  
  326. pascal    short    GetDirID(short vRefNum, long dirID,
  327.                          char* name, long *theDirID, Boolean *isDirectory)
  328. {
  329.     CInfoPBRec pb;
  330.     short error;
  331.  
  332.     pb.hFileInfo.ioNamePtr = (StringPtr) name;
  333.     pb.hFileInfo.ioVRefNum = vRefNum;
  334.     pb.hFileInfo.ioDirID = dirID;
  335.     pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  336.     error = PBGetCatInfoSync(&pb);
  337.     *theDirID = pb.hFileInfo.ioDirID;
  338.     *isDirectory = (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
  339.     return ( error );
  340. }
  341.  
  342.  
  343. #endif
  344.